(function (exports) {
    var $ = null;

    var RedBagLayer = cc.Layer.extend({
        addListener: function (code, listener) {
            network.addListener(code, listener, this);
        },
        onEnter: function () {
            cc.Layer.prototype.onEnter.call(this);
            network.sendProto(1047, {
                UserID: gameData.uid + '',
                GameID: app.area,
                AppID: app.appId + '',
                Role: 'user'
            });
        },
        onExit: function () {
            cc.Layer.prototype.onExit.call(this);
            network.removeListenersForTarget(this);
        },
        initGetRedNode: function () {
            var that = this;
            TouchUtil.setOnclickListener($('node_get_redbag.btn_close_tip'), function (node) {
                $('node_get_redbag').setVisible(false);
            });
            TouchUtil.setOnclickListener($('node_get_redbag.btn_share'), function (node) {
                var Url = 'http://pay.yayayouxi.com/mochuang/red_packet/index.html';
                Url += '?area=' + app.area;
                Url += '&userId=' + gameData.uid;
                Url += '&token=' + that._token;
                Url += '&role=4';
                Url += '&appid=' + app.appId;
                Url += '&amount=' + that._amount;
                Url += '&link=' + app.shareDownLoadUrl;
                WeChatUtil.shareUrl(Url, '好友给您送来了红包大礼', '您的好友给您送来了红包大礼，快来看看!', 0, getCurTimestamp() + gameData.uid);
                that.parent.removeFromParent();
            });
        },
        ctor: function () {
            this._super();
            var that = this;

            var scene = loadNodeCCS(res.RedBagLayer_json, this);
            $ = create$(this.getChildByName('Layer'));
            this._$ = $;

            // var size = $('bg').getContentSize();
            // var piaodai = getCreateRoomAni(7);
            // piaodai.setPosition(size.width/2, size.height - 50);
            // $('bg').addChild(piaodai);
            $('lb_red_num').setVisible(false);
            this.initGetRedNode();
            $('node_get_redbag').setVisible(false);
            $('node_get_redbag').setLocalZOrder(100);
            $('redbag_0').setVisible(false);
            this.addListener(1048, function (data) {
                hideLoading();

                var MyBalance = data.Balance / 100 + '';
                var lb_red_num = $('lb_red_num');
                lb_red_num.setString(MyBalance);
                lb_red_num.setVisible(true);
                // $('img_yuan').setPositionX(lb_red_num.getPositionX()+lb_red_num.getBoundingBox().width/2);

                var CfgInfo = data.CfgInfo;
                var redbag_0 = $('redbag_0');
                // var disOfNum = [0, 0, 225, 193, 177];//红包之间的间距
                var posArr = [cc.p(520, 230), cc.p(880, 230), cc.p(450, 150), cc.p(970, 150), cc.p(690, 100)];
                var srcArr = [3, 3, 2, 2, 1];
                for (var i = 0; i < CfgInfo.length; i++) {
                    (function (i) {
                        var num = i + 1;
                        var node = $('redbag_' + num);
                        if (!node) {
                            var node = duplicateNode(redbag_0);
                            node.setPositionY(redbag_0.getPositionY());
                            node.setName('redbag_' + num);
                            redbag_0.getParent().addChild(node);
                        }
                        node.setVisible(true);
                        // node.setPositionX(startPosx[CfgInfo.length - 1] + disOfNum[CfgInfo.length - 1] * i);
                        var r = Math.random();
                        node.setPosition(posArr[i] || posArr[0]);

                        var balance = CfgInfo[i].MoneyGear / 100 + '';
                        //设置红包金额 居中
                        var lb_red_num = $('read_bg_num', node);
                        lb_red_num.setString('');

                        lb_red_num.setString(balance + "元");
                        var srct = srcArr[i] || 3;
                        $('btn_duihuan', node).setTexture('modules/main/hall_common_module/res/otherFunc/redbag_layer/duihuan'+srct+'.png');
                        // $('img_yuan2', node).setPositionX(lb_red_num.getPositionX()+lb_red_num.getContentSize().width/2);;

                        var func = function () {
                            if (CfgInfo[i].state == 0) {
                                showAlert('金额不足' + balance + '元，无法兑换!');
                                return;
                            }
                            var data = {
                                UserID: gameData.uid + '',
                                GameID: app.area + '',
                                MoneyGear: CfgInfo[i].MoneyGear,
                                Role: 'user',
                                AppID: app.appId + ''
                            };
                            that._amount = data.MoneyGear;
                            network.sendProto(1051, data);
                        };
                        TouchUtil.setOnclickListener(node, function (node) {
                            func();
                        });

                        TouchUtil.setOnclickListener($('btn_duihuan', node), function (node) {
                            func();
                        });

                    })(i);
                }
            }, that);
            this.addListener(1052, function (data) {
                that._url = data.Url;
                that._token = data.Token;
                $('node_get_redbag').setVisible(true);
            }, that);

            TouchUtil.setOnclickListener($('btn_close'), function (node) {
                that.parent.removeFromParent(true);
            });

            TouchUtil.setOnclickListener($('btn_jilu'), function (node) {
                that.addChild(new RecordLayer(), 10);
            });
        },
    });
    exports.RedBagLayer = RedBagLayer;

})(window);